const express = require("express");
const router = express.Router();
const bannerModel = require("../db/model/bannerModel");
// 添加接口
/**
 * @api {post} /banner/create 增加轮播图
 * @apiName /banner/create
 * @apiGroup banner
 *
 * @apiParam {String} link 链接.
 * @apiParam {String} path 图片的链接.
 *
 * @apiSuccess {String} msg  信息.
 * @apiSuccess {String} code 状态码.
 */
router.post("/create", async (req, res) => {
  const { link, path } = req.body;
  if (!link || !path) return res.send({ code: -2, msg: "请传入完整参数哦" });
  const result = await bannerModel.insertMany({ link, path });
  if (!result) return res.send({ code: -1, msg: "轮播图添加失败" });
  res.send({ code: 0, msg: "轮播图添加成功" });
});

// 获取所有数据接口
/**
 * @api {get} /banner/allList 获取所有数据接口
 * @apiName /banner/allList
 * @apiGroup banner
 *
 * @apiParam {Number} page 分页的页码数.
 * @apiParam {Number} pageSize 分页的数量.
 *
 * @apiSuccess {String} msg  信息.
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {Number} count 数据总条数.
 * @apiSuccess {Array} list 数据信息.
 * @apiSuccess {String} list.item.path 轮播图的图片链接.
 */
router.get("/allList", async (req, res) => {
  // limit 限制数量条数
  // skip 跳过几条
  const { page = 1, pageSize = 2 } = req.query;
  const count = await bannerModel.count(); // 获取总数量
  const result = await bannerModel
    .find()
    .limit(pageSize)
    .skip((page - 1) * pageSize);
  if (!result) return res.send({ code: -1, msg: "轮播图查询失败咯" });
  res.send({ code: 0, msg: "轮播图查询成功啦", list: result, count });
});

// 根据id更新录播图
/**
 * @api {post} /banner/update 根据id更新录播图
 * @apiName /banner/update
 * @apiGroup banner
 *
 * @apiParam {id} id id.
 * @apiParam {pushState} pushState 发布的状态.
 *
 * @apiSuccess {String} msg  信息.
 * @apiSuccess {String} code 状态码.
 */
router.post("/update", async (req, res) => {
  const { id, pushState } = req.body;
  // 根据id修改更新状态
  const result = await bannerModel.updateOne(
    { _id: id },
    { $set: { pushState } }
  );
  if (!result) return res.send({ code: -1, msg: "轮播图更新失败" });
  res.send({ code: 0, msg: "轮播图更新成功咯" });
});

// 根据id删除录播图
/**
 * @api {post} /banner/del 根据id删除录播图
 * @apiName /banner/del
 * @apiGroup banner
 *
 * @apiParam {id} id id .
 *
 * @apiSuccess {String} msg  信息.
 * @apiSuccess {String} code 状态码.
 */
router.post("/del", async (req, res) => {
  const { id } = req.body;
  const result = await bannerModel.deleteOne({ _id: id });
  if (!result) return res.send({ code: -1, msg: "轮播图删除失败" });
  res.send({ code: 0, msg: "轮播图删除成功" });
});

// 查询已发布的接口信息
/**
 * @api {get} /banner/getPushBanner 查询已发布的接口信息
 * @apiName /banner/getPushBanner
 * @apiGroup banner
 *
 * @apiSuccess {String} msg  信息.
 * @apiSuccess {String} code 状态码.
 */
router.get("/getPushBanner", async (req, res) => {
  const result = await bannerModel.find({ pushState: "1" });
  if (!result) return res.send({ code: -1, msg: "查询失败" });
  res.send({ code: 0, msg: "查询成功", list: result });
});

// 模糊搜索 link
/**
 * @api {get} /banner/searchLink 模糊搜索
 * @apiName /banner/searchLink
 * @apiGroup banner
 *
 * @apiParam {kw} kw 搜索关键字.
 *
 * @apiSuccess {String} msg  信息.
 * @apiSuccess {String} code 状态码.
 * @apiSuccess {String} list 返回的数据列表.
 */
router.get("/searchLink", async (req, res) => {
  const { kw } = req.query;
  // 通过关键字创建正则表达式
  const reg = new RegExp(kw, "i");
  // 查询数据库的时候 通过$regex 过滤符合正则表达式的信息
  const result = await bannerModel.find({ link: { $regex: reg } });
  if (!result) return res.send({ code: -1, msg: "查询失败" });
  res.send({ code: 0, msg: "查询成功", list: result });
});

module.exports = router;
